/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.control;

import org.eclipse.ui.PlatformUI;
import org.mags.common.EngineContext;
import org.mags.remas.control.commons.ICrudActions;
import org.mags.remas.model.entity.EntityCategory;
import org.mags.remas.model.entity.EntityReference;
import org.mags.remas.model.entity.EntityRelationship;
import org.mags.remas.view.dialogs.RelationshipTreeDialog;
import org.mags.remas.view.dialogs.SingletonRelationshipTreeDialog;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class ReferenceController implements ICrudActions {
	private final EntityRelationship entityRelationship;
	private boolean from = true;
	
	public boolean isFrom() {
		return from;
	}
	public void setFrom(boolean from) {
		this.from = from;
	}
	public ReferenceController(EntityRelationship entityRelationship) {
		this.entityRelationship = entityRelationship;		
	}
	@Override
	public Object create(Object object) {		
		SingletonRelationshipTreeDialog treeDialog = SingletonRelationshipTreeDialog.getInstance(PlatformUI.getWorkbench().getDisplay());
		EntityCategory entityCategory = entityRelationship.getEntity().getEntityType().getEntityCategory();		
		if(entityCategory!=null)
			treeDialog.setSelectedEntityCategory(entityCategory);
		treeDialog.setText("select a reference");
		treeDialog.setEnabledNoLinkButton(false);
		treeDialog.show();
		treeDialog.setEnabledNoLinkButton(true);
		if(treeDialog.getStatus()==RelationshipTreeDialog.STATUS_CANCELED)
			return null;
		EntityRelationship selected = treeDialog.getSelectedEntityRelationship();
		EntityReference entityReference = new EntityReference();
		if(from) {
			entityReference.setEntity(entityRelationship.getEntity());
			entityReference.setReference(selected.getEntity());
		} else {
			entityReference.setEntity(selected.getEntity());
			entityReference.setReference(entityRelationship.getEntity());			
		}
		entityReference.setDescription("");
		entityReference.setReferenceStatus(EngineContext.getDataContext().getEntityProviderFactory().getReferenceStatusProvider().getActiveStatus());
		EngineContext.getDataContext().getEntityProviderFactory().getEntityReferenceProvider().create(entityReference);
		return entityReference;
	}
	@Override
	public void delete(Object object) {
		EntityReference entityReference = (EntityReference)object;
		EngineContext.getDataContext().getEntityProviderFactory().getEntityReferenceProvider().delete(entityReference);								
	}
	@Override
	public Object getObjects(Object parent) {
		if(from)
			return EngineContext.getDataContext().getEntityProviderFactory().getEntityReferenceProvider().getByEntityId(entityRelationship.getEntity().getEntityId());
		else
			return EngineContext.getDataContext().getEntityProviderFactory().getEntityReferenceProvider().getByReferenceId(entityRelationship.getEntity().getEntityId());
	}
	@Override
	public void update(Object object) {
		EntityReference entityReference = (EntityReference)object;
		EngineContext.getDataContext().getEntityProviderFactory().getEntityReferenceProvider().update(entityReference);
		entityReference.setReferenceStatus(EngineContext.getDataContext().getEntityProviderFactory().getReferenceStatusProvider().getById(entityReference.getReferenceStatus().getReferenceStatusId()));
	}
}
